// 前端开发服务器
const express = require('express');
const path = require('path');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
const PORT = 3000;

// 解析JSON请求体
app.use(express.json());

// API代理到后端
app.use('/api', createProxyMiddleware({
  target: 'http://localhost:5001',
  changeOrigin: true,
  pathRewrite: {
    '^/api': '/api'
  }
}));

// 服务静态文件
app.use(express.static(path.join(__dirname, 'client/dist')));

// 处理SPA路由
app.get(/.*/, (req, res) => {
  res.sendFile(path.join(__dirname, 'client/dist', 'index.html'));
});

app.listen(PORT, () => {
  console.log(`前端开发服务器运行在端口 ${PORT}`);
});
